'''
Created on Feb 19, 2011

@author: matt
'''

import unittest
import urllib
#import urllib2
#import json
from httplib2 import Http

import simplejson as json

from xml.sax.saxutils import unescape

class OAuthTest(unittest.TestCase):

    def setUp(self):
        # Note: the unit testing framework appears to call setUp() and tearDown()
        # before/after *each* test method is called, rather than calling
        # setUp() once, running all the tests, then invoking shutDown()
        print '-- OAuthTest.setUp() invoked'


    def tearDown(self):
        # Only need this if setUp() is specified
        print '-- OAuthTest.tearDown() invoked'


    def testDummy(self):
        print '-- OAuthTest.testDummy() invoked'

    def testName(self):
        # Note: I'm not entirely sure what conventions (if any) the result is
        # supposed to adhere to. So I'll just return some text and hope for
        # the best...
        return 'OAuthTest'

    # TODO: write test cases to verify that the basic OAuth operations work

class TwitterTest(unittest.TestCase):
    
    def setUp(self):
        print '-- TwitterTest.setUp() invoked'
    
    def tearDown(self):
        print '-- TwitterTest.tearDown() invoked'
     
    def testSearch(self):
        print '-- TwitterTest.testSimple() invoked'
        
        base_url = 'http://search.twitter.com/search.json'
        
        replies_per_page = 25
        
        search_opts_dict = {
            'q' : 'ocean',  # the search term
            'rpp' : replies_per_page,  # replies per page
            'lang' : 'en' # language
        }
        
        # build the full URL
        search_opts = urllib.urlencode(search_opts_dict)
        
        # Note: we could specify a dictionary that contains our HTTP headers,
        # but we don't need to do that now
        
        http = Http()
        
        # build the HTTP request
#        request = urllib2.Request(base_url, search_opts)
        
        # send the request and get the response
        response, content = http.request(base_url, "POST", search_opts)
        
        # print out the string we got back
#        returned_json = response.read()
        
        print '-- SEARCH OPTS is %s' % (search_opts)
        print '-- CONTENT is %s' % (content)
        response_dict = json.loads(content)
        
        # TODO: replace this with a more meaningful test
        self.assertTrue(response_dict)
        self.assertIn('results', response_dict)
        self.assertEqual(replies_per_page, len(response_dict['results']))
        
        # The actual tweet text is stored in:
        #    results > (each result) > text
        
        # TODO: remove this after testing
        for tweet in response_dict['results']:
            # the unescape() replaces escaped values (e.g., &amp) with their
            # single-character equivalents
            try:
                
                unescaped_tweet = unescape(tweet['text'], {"&apos;": "'", "&quot;": '"'})     
                print 'From', tweet['from_user'], '-', unescaped_tweet
            except UnicodeEncodeError:
                print '-- Caught UnicodeEncodeError'
                
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()